package com.jojo.bubble.controller;

import com.jojo.bubble.entity.*;
import com.jojo.bubble.service.*;
import com.jojo.bubble.common.domain.AjaxResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.ObjectUtils;
import com.jojo.bubble.common.enums.JsonResultEnum;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 订单表(Order)表控制层
 *
 * @author jojo
 * @since 2023-08-08 13:43:20
 */
@RestController
@RequestMapping("/admin/order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;
    @Resource
    private AgentService agentService;

    @Resource
    private ShopService shopService;

    @Resource
    private DevService devService;
    @Resource
    private UserService userService;

    @GetMapping("/list")
    public AjaxResult getList(Order record) throws ParseException {
        if (StringUtils.hasLength(record.getTimeRange())) {
            String[] time = record.getTimeRange().split("-");

            Date startTime = new SimpleDateFormat("yyyy/MM/dd").parse(time[0].trim());
            Date endTime = new SimpleDateFormat("yyyy/MM/dd").parse(time[1].trim());
            record.setStartTime(startTime);
            record.setEndTime(endTime);

        }
        List<Order> orderList = orderService.getList(record);
        for (Order order : orderList) {
            User user = userService.getInfo(order.getUserId());
            if (!ObjectUtils.isEmpty(user)) {
                order.setUsername(user.getNickname());
            }
            Dev dev = devService.getInfo(order.getDevId());
            if (!ObjectUtils.isEmpty(dev)) {
                order.setDevCode(dev.getCode());
            }
            Agent agent = agentService.getInfo(order.getAgentId());
            if (!ObjectUtils.isEmpty(agent)) {
                order.setAgentName(agent.getName());
                order.setAgentPhone(agent.getPhone());
            } else {
                order.setAgentName("");
                order.setAgentPhone("");
            }
            Shop shop = shopService.getInfo(order.getShopId());
            if (!ObjectUtils.isEmpty(shop)) {
                order.setShopName(shop.getName());
            } else {
                order.setShopName("");
            }
        }
        return AjaxResult.success(orderList);
    }

    @GetMapping("/info")
    public AjaxResult getInfo(Integer id) {
        Order order = orderService.getInfo(id);
        return AjaxResult.success(order);
    }

    @PostMapping("/insert")
    public AjaxResult insert(Order record) {
        Dev dev = devService.getInfo(record.getDevId());
        if (ObjectUtils.isEmpty(dev)) {
            return AjaxResult.error("设备未连接");
        }
        if (dev.getStatus() != 1) {
            return AjaxResult.error("该设备无法运行");
        }
        orderService.insert(record);
        return AjaxResult.success();
    }

    @PostMapping("/update")
    public AjaxResult update(Order record) {
        Order order = orderService.getInfo(record.getId());
        if (ObjectUtils.isEmpty(order)) {
            return AjaxResult.error(JsonResultEnum.RECORD_NULL);
        }
        orderService.update(record);
        return AjaxResult.success();
    }

    @PostMapping("/delete")
    public AjaxResult delete(int id) {
        orderService.delete(id);
        return AjaxResult.success();
    }
}

